<?php

declare(strict_types=1);

namespace app\admin\model;

use think\Model;

class Customer extends Model
{
    // 定义数据表名
    protected $name = 'customer';

    // 定义主键
    protected $primaryKey = 'id';

    // 定义自动时间戳
    protected $autoIncrement = true;

    // 定义创建时间字段名
    protected $createTime = 'create_time';

    // 定义更新时间字段名
    protected $updateTime = 'update_time';

    // 定义自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 隐藏属性，防止原始字段被覆盖
    // protected $hidden = ['creator', 'updater'];

    // 定义数据类型
    protected $dataType = [
        'status' => 'int',
        'stage' => 'int',
        'tags' => 'string',
        'loss' => 'string',
        'curator' => 'string',
        'custype' => 'string',
        'cusnature' => 'string',
        'source' => 'string',
        'att_path' => 'string',
        'remark' => 'string',
        'is_del' => 'int',
    ];

    // 定义默认值
    protected $default = [
        'status' => 0,
        'stage' => 0,
        'tags' => '',
        'loss' => '',
        'curator' => '',
        'custype' => '',
        'cusnature' => '',
        'source' => '',
        'att_path' => '',
        'remark' => '',
        'is_del' => 0,
    ];

    // 定义关联关系
    protected $relation = [
        'creator' => ['user', 'hasOne'],
        'updater' => ['user', 'hasOne'],
    ];

    // 定义一个通用的访问器来获取属性值
    protected $attributes = [
        'status' => [
            1 => '无效客户',
            2 => '成交客户',
            3 => '忠诚客户',
        ],
        'stage' => [
            1 => '流失',
            2 => '确认需求',
            3 => '成交',
        ],
        'custype' => [
            1 => '零售客户',
            2 => '渠道客户',
            3 => '高级渠道客户',
            4 => '批发客户',
            5 => '网站客户',
            6 => 'kol客户',
            99 => '其他',
        ],
        'cusnature' => [
            1 => '企业客户',
            2 => '个人客户',
            99 => '其他',
        ],
        'source' => [
            1 => '社媒社群 ',
            2 => '官网直客',
            3 => '抓取',
            4 => '平台网店',
            5 => '网络推广',
            6 => '电话销售',
            7 => '渠道代理',
            8 => '线下自寻',
            9 => '其他',
        ],


        'login_status' => [
            1 => '正常',
            2 => '锁定'
        ],
        'is_login' => [
            1 => '登陆',
            2 => '不登陆'
        ]
    ];

    public function getAttr($name)
    {
        // 检查是否是要处理的特殊属性
        if (strpos($name, '_value') !== false) {
            $originalField = str_replace('_value', '', $name);
            if (isset($this->attributes[$originalField])) {
                $value = $this->getData($originalField);
                return $this->attributes[$originalField][$value] ?? '未知';
            }
        }

        // 如果不是特殊属性，调用父类的方法
        return parent::getAttr($name);
    }


    public function getFieldEnum($field)
    {
        if ('ALL' === strtoupper($field)) {
            return [
                'status' => $this->attributes['status'],
                'stage' => $this->attributes['stage'],
                'custype' => $this->attributes['custype'],
                'cusnature' => $this->attributes['cusnature'],
                'source' => $this->attributes['source'],
                'login_status' => $this->attributes['login_status'],
                'is_login' => $this->attributes['is_login'],
            ];
        } else {
            return [$field => $this->$field];
        }
    }


    public function creator()
    {
        return $this->belongsTo('Operator', 'creator', 'id');
    }
    public function country()
    {
        return $this->belongsTo('RegionCountry', 'country_id', 'id');
    }
    public function city()
    {
        return $this->belongsTo('RegionCity', 'city_id', 'id');
    }


    public function getCreatorValueAttr($value, $data)
    {
        $creator = $this->getRelation('creator');
        if (!$creator) {
            $creator = $this->creator()->find();
        }
        return $creator ? $creator->cname : null;
    }

    public function updater()
    {
        return $this->belongsTo('Operator', 'updater', 'id');
    }
    public function getUpdaterValueAttr($value, $data)
    {
        $updater = $this->getRelation('updater');
        if (!$updater) {
            $updater = $this->creator()->find();
        }
        return $updater ? $updater->cname : null;
    }

    public function getOwnerValueAttr($value, $data)
    {
        if (is_null($data['owner']) || '' == $data['owner']) {
            return [];
        }

        $owners = Operator::where('id', $data['owner'])->field('cname')->select()->toArray();
        return implode(',', array_column($owners, 'cname'));
    }
    public function getPartnerValueAttr($value, $data)
    {
        if (is_null($data['partner']) || '' == $data['partner']) {
            return '';
        }
        $partnerIds = explode(',', $data['partner']);

        $partnerIds = array_filter($partnerIds, 'strlen');
        $partners = Operator::whereIn('id', $partnerIds)->field('cname')->select()->toArray();

        return  implode(',', array_column($partners, 'cname'));
    }
}
